package com.gym.controller.action.customer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gym.model.dao.ConnessioneException;
import com.gym.model.dao.DAOCustomer;
import com.gym.model.dao.DatoNonTrovatoException;
import com.gym.model.dao.MetodoDaoException;
import com.gym.model.entity.Customer;
import com.gym.model.entity.User;

public class RecuperaCustomer extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) {
	
		try {
			DAOCustomer daoCustomer = new DAOCustomer();
			
			//Recupera il customer a partire dall'oggetto User presente in sessione
			Customer c = daoCustomer.selectByUser((User)request.getSession().getAttribute("user"));
	
			request.setAttribute("customer", c);
			
			return mapping.findForward("success");
			
		} catch (ConnessioneException | MetodoDaoException | DatoNonTrovatoException e) {
			request.setAttribute("error", e.getMessage());
			return mapping.findForward("failure");
		}
	}
}
